using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCounterAttackState : PlayerState
{
    private bool canCreateClone;// 限制格挡创造幻影，只能创造一个
    public PlayerCounterAttackState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {
    }

    public override void Enter()
    {
        base.Enter();
        canCreateClone = true;
        stateTimer = player.counterAttackDuration;// 设置格挡时间
        player.anim.SetBool("SuccessfulCounterAttack", false);
    }

    public override void Exit()
    {
        base.Exit();
    }

    public override void Update()
    {
        base.Update();
        player.SetZeroVelocity();
        // 获取攻击圆圈内所有碰撞体
        Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);

        foreach (var hit in colliders)
        {
            // 判断敌人
            if (hit.GetComponent<Enemy>() != null)
            {
                if (hit.GetComponent<Enemy>().IsCanBeStunned())// 敌人是否处在攻击可被格挡状态
                {
                    stateTimer = 10;// 重置时间防止退出状态
                    player.anim.SetBool("SuccessfulCounterAttack", true);// 格挡成功动画
                    player.skill.parry.UseSkill(); //使用格挡恢复技能
                    if (canCreateClone)// 限制格挡创造幻影，只执行一次
                    {
                        canCreateClone = false;
                        // 由克隆技能判断是否能生成幻影
                        player.skill.parry.MakeMirageOnParry(hit.transform);
                    }
                }
            }
        }

        if (stateTimer < 0 || triggerCalled)//格挡失败或格挡成功动画结束
        {
            stateMachine.ChangeState(player.idleState);
        }
    }
}
